﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

namespace Web.myadmin
{
    /// <summary>
    /// 本程序为给济宁恒久汽修厂特制的信息管理系统
    /// 作者：田建军
    /// 邮箱：buzhizhe@qq.com
    /// qq号：40061052
    /// </summary>
    public partial class carMaintainInfo : System.Web.UI.Page
    {
        BZZ.Model.carMaintainInfo mCarMaintainInfo = new BZZ.Model.carMaintainInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            //SessionHelp.AssertLogin(this); //登录验证
            showTheRightRegion();
        }

        #region 自定义函数

        /// <summary>
        /// 函数功能：示例函数
        /// 作者：buzhizhe 创建时间：2011.6.22
        /// 修改者：buzhizhe 最后修改时间：2011.6.22
        /// </summary>
        protected void YourFunction()
        {
            ///
        }


        #endregion

        #region 信息列表和查询
        /// <summary>
        /// 查询数据
        /// </summary>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            showList();
        }

        /// <summary>
        /// 数据列表
        /// </summary>
        protected void showList()
        {
            Dictionary<string, string> dicCondition = new Dictionary<string, string>()
            {
                //{"id",txtQryId.Text},
                {"owner",txtQryOwner.Text},
                //{"telephone",txtQryTelephone.Text},
                {"carNo",txtQryCarNo.Text}
                //{"carType",txtQryCarType.Text},
                //{"loadNo",txtQryLoadNo.Text},
                //{"shelfNo",txtQryShelfNo.Text},
                //{"addDate",txtQryAddDate.Text},
                //{"weights",txtQryWeights.Text},
                //{"cardNo",txtQryCardNo.Text},
                //{"date1",txtQryDate1.Text},
                //{"date2",txtQryDate2.Text},
                //{"date3",txtQryDate3.Text},
                //{"endDate",txtQryEndDate.Text},
                //{"sortDate",txtQrySortDate.Text}
            };

             #region 分页控件绑定
             int currentPage = 1; //当前页码
             int.TryParse(Request.QueryString["p"], out currentPage);
             string pageHtml; //分页导航html
             DataTable dtable = mCarMaintainInfo.getOnePage(dicCondition, currentPage, out pageHtml, 30);
             if (dtable != null)
             {
                 rptInfoList.DataSource = dtable;
                 rptInfoList.DataBind();
                 litPager.Text = pageHtml;
             }
             else
             {
                 litPromptMessage.Text = "对不起，没有符合条件的数据。";
             }
             #endregion

        }
        #endregion


        private void CreateExcel(DataTable dt, string fileName)
        {
            HttpResponse resp;
            resp = Page.Response;
            resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            resp.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
            resp.ContentType = "application/vnd.ms-excel";
            string colHeaders = "", ls_item = "";

            DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
            int i = 0;
            int cl = dt.Columns.Count;

            //取得数据表各列标题，各标题之间以t分割，最后一个列标题后加回车符
            for (i = 0; i < cl; i++)
            {
                if (i == (cl - 1))//最后一列，加n
                {
                    colHeaders += dt.Columns[i].Caption.ToString() + "\n";
                }
                else
                {
                    colHeaders += dt.Columns[i].Caption.ToString() + "\t";
                }
            }
            resp.Write(colHeaders);
            //向HTTP输出流中写入取得的数据信息

            //逐行处理数据
            foreach (DataRow row in myRow)
            {
                //当前行数据写入HTTP输出流，并且置空ls_item以便下行数据
                for (i = 0; i < cl; i++)
                {
                    if (i == (cl - 1))//最后一列，加n
                    {
                        ls_item += row[i].ToString() + "\n";
                    }
                    else
                    {
                        ls_item += row[i].ToString() + "\t";
                    }

                }
                resp.Write(ls_item);
                ls_item = "";

            }
            resp.End();
        }

        /// <summary>
        /// 输入字符串格式成日期或null
        /// </summary>
        /// <param name="strData"></param>
        /// <returns></returns>
        private DateTime? formatDate(string strData)
        {
            DateTime? dt = null;
            try
            {
                dt = DateTime.Parse(strData);
            }
            catch { }
            return dt;
        }
        /// <summary>
        /// 获取最后一个到期日期
        /// </summary>
        /// <param name="dt1"></param>
        /// <param name="dt2"></param>
        /// <param name="dt3"></param>
        /// <returns></returns>
        private DateTime? maxDate(DateTime? dt1, DateTime? dt2, DateTime? dt3)
        {
            DateTime? dt = DateTime.Today.AddYears(-100);
            if (dt1 != null) dt = dt1;
            if (dt2 != null) dt = dt2;
            if (dt3 != null) dt = dt3;
            return dt;
        }
        /// <summary>
        /// 不为空的日期，显示成年月日格式
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private string displayDate(DateTime? dt)
        {
            if (dt == null) return "";
            else return Convert.ToDateTime(dt).ToString("yyyy-MM-dd");
        }
        /// <summary>
        /// 不为空的日期，按年月日显示在list
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public string displayDate(object dt)
        {
            DateTime? dtt = null;
            try { dtt = Convert.ToDateTime(dt); }
            catch { return ""; }

            if (dtt == null) return "";
            else return Convert.ToDateTime(dtt).ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// 以红色显示10天内到期的信息
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public string displayDays(object d)
        {
            string s = "-";
            try
            {
                int n = Convert.ToInt32(d);
                if (n < 10) s = "<font color=red>" + n.ToString() + "</font>";
                else s = n.ToString();
            }
            catch { }
            return s;
        }

        #region 添加、修改、删除操作
        /// <summary>
        /// 添加修改操作
        /// </summary>
        protected void btnSumit_Click(object sender, EventArgs e)
        {
            int Id ;	//id
            int.TryParse(txtId.Text, out Id);
            if (Id == 0) { saveAdd(); }
            else { saveUpdate(Id); }
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        private void saveAdd()
        {
            #region 数据类型验证
            string varOwner;	//车辆所有人
            string varTelephone;	//联系电话
            string varCarNo;	//车牌号码
            string varCarType;	//车型
            string varLoadNo;	//发动机号
            string varShelfNo;	//大架号
            DateTime? varAddDate ;	//登记日期
            string varWeights;	//吨位
            string varCardNo;	//二维卡号
            DateTime? varDate1 ;	//到期时间1
            DateTime? varDate2 ;	//到期时间2
            DateTime? varDate3 ;	//到期时间3
            DateTime? varEndDate ;	//营运到期
            DateTime? varSortDate ;	//排序时间
            try
            {
               varOwner =txtOwner.Text.Trim();	//车辆所有人
               varTelephone =txtTelephone.Text.Trim();	//联系电话
               varCarNo =txtCarNo.Text.Trim();	//车牌号码
               varCarType =txtCarType.Text.Trim();	//车型
               varLoadNo =txtLoadNo.Text.Trim();	//发动机号
               varShelfNo =txtShelfNo.Text.Trim();	//大架号
               varAddDate = formatDate(txtAddDate.Text.Trim());	//登记日期
               varWeights =txtWeights.Text.Trim();	//吨位
               varCardNo =txtCardNo.Text.Trim();	//二维卡号
               varDate1 =formatDate(txtDate1.Text.Trim());	//到期时间1
               varDate2 =formatDate(txtDate2.Text.Trim());	//到期时间2
               varDate3 =formatDate(txtDate3.Text.Trim());	//到期时间3
               varEndDate =formatDate(txtEndDate.Text.Trim());	//营运到期
               varSortDate = maxDate(varDate1, varDate2, varDate3);
            }
            catch
            {
                LIB.JsWrite.AlertAndGoback(this, "输入数据格式不合法，请检查输入数据。");
                return;
            } 
            #endregion

            #region 赋值model
                mCarMaintainInfo.owner = varOwner;	//车辆所有人
                mCarMaintainInfo.telephone = varTelephone;	//联系电话
                mCarMaintainInfo.carNo = varCarNo;	//车牌号码
                mCarMaintainInfo.carType = varCarType;	//车型
                mCarMaintainInfo.loadNo = varLoadNo;	//发动机号
                mCarMaintainInfo.shelfNo = varShelfNo;	//大架号
                mCarMaintainInfo.addDate = varAddDate;	//登记日期
                mCarMaintainInfo.weights = varWeights;	//吨位
                mCarMaintainInfo.cardNo = varCardNo;	//二维卡号
                mCarMaintainInfo.date1 = varDate1;	//到期时间1
                mCarMaintainInfo.date2 = varDate2;	//到期时间2
                mCarMaintainInfo.date3 = varDate3;	//到期时间3
                mCarMaintainInfo.endDate = varEndDate;	//营运到期
                mCarMaintainInfo.sortDate = varSortDate;	//排序时间
            #endregion

            #region 保存数据
            if (mCarMaintainInfo.Add() > 0) 
                { LIB.JsWrite.AlertAndGo(this, "添加成功", "?cmd=list"); }
            else 
                { LIB.JsWrite.AlertAndGoback(this, "操作失败"); }
            #endregion

        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="varId"></param>
        private void saveUpdate(int varId)
        {
            #region 数据类型验证
            string varOwner;	//车辆所有人
            string varTelephone;	//联系电话
            string varCarNo;	//车牌号码
            string varCarType;	//车型
            string varLoadNo;	//发动机号
            string varShelfNo;	//大架号
            DateTime? varAddDate ;	//登记日期
            string varWeights;	//吨位
            string varCardNo;	//二维卡号
            DateTime? varDate1 ;	//到期时间1
            DateTime? varDate2 ;	//到期时间2
            DateTime? varDate3 ;	//到期时间3
            DateTime? varEndDate ;	//营运到期
            DateTime? varSortDate ;	//排序时间
            try
            {
               varOwner =txtOwner.Text.Trim();	//车辆所有人
               varTelephone =txtTelephone.Text.Trim();	//联系电话
               varCarNo =txtCarNo.Text.Trim();	//车牌号码
               varCarType =txtCarType.Text.Trim();	//车型
               varLoadNo =txtLoadNo.Text.Trim();	//发动机号
               varShelfNo =txtShelfNo.Text.Trim();	//大架号
               varAddDate =formatDate(txtAddDate.Text.Trim());	//登记日期
               varWeights =txtWeights.Text.Trim();	//吨位
               varCardNo =txtCardNo.Text.Trim();	//二维卡号
               varDate1 =formatDate(txtDate1.Text.Trim());	//到期时间1
               varDate2 =formatDate(txtDate2.Text.Trim());	//到期时间2
               varDate3 =formatDate(txtDate3.Text.Trim());	//到期时间3
               varEndDate =formatDate(txtEndDate.Text.Trim());	//营运到期
               varSortDate = maxDate(varDate1, varDate2, varDate3);
            }
            catch
            {
                LIB.JsWrite.AlertAndGoback(this, "输入数据格式不合法，请检查输入数据。");
                return;
            }
            #endregion

            #region 赋值model
            mCarMaintainInfo.id = varId;
            mCarMaintainInfo.owner = varOwner;	//车辆所有人
            mCarMaintainInfo.telephone = varTelephone;	//联系电话
            mCarMaintainInfo.carNo = varCarNo;	//车牌号码
            mCarMaintainInfo.carType = varCarType;	//车型
            mCarMaintainInfo.loadNo = varLoadNo;	//发动机号
            mCarMaintainInfo.shelfNo = varShelfNo;	//大架号
            mCarMaintainInfo.addDate = varAddDate;	//登记日期
            mCarMaintainInfo.weights = varWeights;	//吨位
            mCarMaintainInfo.cardNo = varCardNo;	//二维卡号
            mCarMaintainInfo.date1 = varDate1;	//到期时间1
            mCarMaintainInfo.date2 = varDate2;	//到期时间2
            mCarMaintainInfo.date3 = varDate3;	//到期时间3
            mCarMaintainInfo.endDate = varEndDate;	//营运到期
            mCarMaintainInfo.sortDate = varSortDate;	//排序时间
            #endregion

            #region 保存数据
            if (mCarMaintainInfo.Update() > 0)
                { LIB.JsWrite.alertAndClose(this, "修改成功"); }
            else
                { LIB.JsWrite.AlertAndGoback(this, "操作失败"); }
            #endregion

        }

        /// <summary>
        /// 删除数据
        /// </summary>
        protected void delete()
        {
            #region 删除操作
            int id = 0;
            int.TryParse(Request.QueryString["id"], out id);
            if (id == 0)
            {
                LIB.JsWrite.Alert(this, "非法的参数");
            }
            else
            {
                if (mCarMaintainInfo.Delete(id) > 0)
                    { LIB.JsWrite.AlertAndGo(this, "删除成功", "?cmd=list"); }
                else
                    { LIB.JsWrite.AlertAndGoback(this, "操作失败"); }
            }
            #endregion
        }
        #endregion

        #region 初始化添加表单
        /// <summary>
        /// 初始化添加表单
        /// </summary>
        protected void initAddForm()
        {
            lblFormTitle.Text = "添加信息";
            if (!IsPostBack)
            {

                txtOwner.Text = "";
                txtTelephone.Text = "";
                txtCarNo.Text = "鲁H";
                txtCarType.Text = "";
                txtLoadNo.Text = "";
                txtShelfNo.Text = "";
                txtAddDate.Text = "";
                txtWeights.Text = "";
                txtCardNo.Text = "";
                txtDate1.Text = "";
                txtDate2.Text = "";
                txtDate3.Text = "";
                txtEndDate.Text = "";
               
            }
        }
        #endregion

        #region 初始化修改表单
        /// <summary>
        /// 初始化修改表单
        /// </summary>
        protected void initEditFrom()
        {
            int id = 0;
            int.TryParse(Request.QueryString["id"], out id);
            lblFormTitle.Text = "修改信息";
            if (!IsPostBack && id>0)
            {
                mCarMaintainInfo = mCarMaintainInfo.GetModel(id);
                txtId.Text = mCarMaintainInfo.id.ToString();
                txtOwner.Text = mCarMaintainInfo.owner.ToString();
                txtTelephone.Text = mCarMaintainInfo.telephone.ToString();
                txtCarNo.Text = mCarMaintainInfo.carNo.ToString();
                txtCarType.Text = mCarMaintainInfo.carType.ToString();
                txtLoadNo.Text = mCarMaintainInfo.loadNo.ToString();
                txtShelfNo.Text = mCarMaintainInfo.shelfNo.ToString();
                txtAddDate.Text =displayDate(mCarMaintainInfo.addDate);
                txtWeights.Text = mCarMaintainInfo.weights.ToString();
                txtCardNo.Text = mCarMaintainInfo.cardNo.ToString();
                txtDate1.Text = displayDate(mCarMaintainInfo.date1);
                txtDate2.Text = displayDate(mCarMaintainInfo.date2);
                txtDate3.Text = displayDate(mCarMaintainInfo.date3);
                txtEndDate.Text = displayDate(mCarMaintainInfo.endDate);
                
            }
        }
        #endregion

        #region 初始化查看表格
        /// <summary>
        /// 初始化查看表格
        /// </summary>
        protected void initViewTable()
        {
            int id = 0;
            int.TryParse(Request.QueryString["id"], out id);
            lblViewTitle.Text = "查看信息";
            if (!IsPostBack && id > 0)
            {
                mCarMaintainInfo = mCarMaintainInfo.GetModel(id);
                lblOwner.Text = mCarMaintainInfo.owner.ToString();
                lblTelephone.Text = mCarMaintainInfo.telephone.ToString();
                lblCarNo.Text = mCarMaintainInfo.carNo.ToString();
                lblCarType.Text = mCarMaintainInfo.carType.ToString();
                lblLoadNo.Text = mCarMaintainInfo.loadNo.ToString();
                lblShelfNo.Text = mCarMaintainInfo.shelfNo.ToString();
                lblAddDate.Text = displayDate(mCarMaintainInfo.addDate);
                lblWeights.Text = mCarMaintainInfo.weights.ToString();
                lblCardNo.Text = mCarMaintainInfo.cardNo.ToString();
                lblDate1.Text = displayDate(mCarMaintainInfo.date1);
                lblDate2.Text = displayDate(mCarMaintainInfo.date2);
                lblDate3.Text = displayDate(mCarMaintainInfo.date3);
                lblEndDate.Text = displayDate(mCarMaintainInfo.endDate);
                lblSortDate.Text = displayDate(mCarMaintainInfo.sortDate);
            }
        }

        #endregion

        #region 公共函数，不需要修改

        /// <summary>
        /// 根据cmd参数判断应该显示哪些部分内容
        /// </summary>
        /// <param name="cmd"></param>
        protected void showTheRightRegion()
        {
            #region show or hide div
            string cmd = Request.QueryString["cmd"];
            switch (cmd)
            {
                case "add": initAddForm(); break;
                case "edit": initEditFrom(); break;
                case "view": initViewTable(); break;
                default: showList(); break;
            }
            
            switch (cmd)
            {
                case "add":
                    divView.Visible = false;
                    divForm.Visible = true;
                    divList.Visible = false;
                    break;
                case "edit":
                    divView.Visible = false;
                    divForm.Visible = true;
                    divList.Visible = false;
                    break;
                case "view":
                    divView.Visible = true;
                    divForm.Visible = false;
                    divList.Visible = false;
                    break;
                default:
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    break;
            }
            #endregion
        }
        #endregion

        protected void btnExport_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> dicCondition = new Dictionary<string, string>()
            {
                //{"id",txtQryId.Text},
                {"owner",txtQryOwner.Text},
                //{"telephone",txtQryTelephone.Text},
                {"carNo",txtQryCarNo.Text}
                //{"carType",txtQryCarType.Text},
                //{"loadNo",txtQryLoadNo.Text},
                //{"shelfNo",txtQryShelfNo.Text},
                //{"addDate",txtQryAddDate.Text},
                //{"weights",txtQryWeights.Text},
                //{"cardNo",txtQryCardNo.Text},
                //{"date1",txtQryDate1.Text},
                //{"date2",txtQryDate2.Text},
                //{"date3",txtQryDate3.Text},
                //{"endDate",txtQryEndDate.Text},
                //{"sortDate",txtQrySortDate.Text}
            };

            DataSet dset = mCarMaintainInfo.GetDataset(dicCondition);
            if (dset != null && dset.Tables.Count > 0)
            {
                string fileName="dataBakup"+DateTime.Today.ToString("yyyyMMdd")+".xls";
                DataTable dt = dset.Tables[0];
                dt.Columns[0].Caption = "编号";
                dt.Columns[1].Caption = "车辆所有人";
                dt.Columns[2].Caption = "联系电话";
                dt.Columns[3].Caption = "车牌号码";
                dt.Columns[4].Caption = "车型";
                dt.Columns[5].Caption = "发动机号";
                dt.Columns[6].Caption = "大架号";
                dt.Columns[7].Caption = "登记日期";
                dt.Columns[8].Caption = "吨位";
                dt.Columns[9].Caption = "二维卡号";
                dt.Columns[10].Caption = "到期时间1";
                dt.Columns[11].Caption = "到期时间2";
                dt.Columns[12].Caption = "到期时间3";
                dt.Columns[13].Caption = "营运到期";
                dt.Columns[14].Caption = "排序时间";

                CreateExcel(dt, fileName);
            }
        }



    }


}


